// This File is part of the "GmailNotifier2" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace Notify2
{
    public partial class About : Form
    {
        static About _default;
        public static About Default
        {
            get
            {
                if (_default == null)
                    _default = new About();
                return _default;
            }
        }

        public About()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void About_Load(object sender, EventArgs e)
        {
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            string s1 = conf.FilePath;
            textBox1.Text = Path.GetDirectoryName(s1);

            DateTime modified = File.GetLastWriteTime(Application.ExecutablePath);
            versionLabel.Text = "v" + Application.ProductVersion + " - " + modified.ToShortDateString() + " " + modified.ToLongTimeString();

            //MessageBox.Show(Path.GetDirectoryName(Application.StartupPath) + "\r\n" + SetupForm.setupPath);
            if (string.Compare(Path.GetDirectoryName(Application.StartupPath), SetupForm.setupPath, true) == 0)
            {
                setupSplitButton.DefaultItem = uninstallToolStripMenuItem;
                setupSplitButton.Text = uninstallToolStripMenuItem.Text;
            }
            else
            {
                setupSplitButton.DefaultItem = runSetupToolStripMenuItem;
                setupSplitButton.Text = runSetupToolStripMenuItem.Text;
            }
        }

        void StartSetup(string filename, string arguments)
        {
            string[] args = Environment.GetCommandLineArgs();
            string temp = Environment.GetEnvironmentVariable("TEMP");
            string setup = Path.Combine(temp, filename);
            SetupUtils.Copy(Application.ExecutablePath, setup);
            Program.ScheduleDeleteUninstallOnReboot(setup);
            ProcessStartInfo processStartInfo = new ProcessStartInfo(setup);
            processStartInfo.Verb = "runas";
            processStartInfo.UseShellExecute = true;
            processStartInfo.ErrorDialog = true;
            processStartInfo.WorkingDirectory = Path.GetDirectoryName(setup);
            processStartInfo.Arguments = arguments;
            Process.Start(processStartInfo);
        }

        private void setupSplitButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ((ToolStripSplitButton)sender).DefaultItem = e.ClickedItem;
            ((ToolStripSplitButton)sender).Text = e.ClickedItem.Text;
        }

        private void runSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartSetup(Path.GetFileName(Application.ExecutablePath), "");
            this.Close();
        }

        private void uninstallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartSetup("uninstall.exe", "/uninstall");
            this.Close();
        }
    }
}
















